package com.taotao.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.content.service.ContentCategoryService;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.EasyUITreeNode;
import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
/**
 * 内容分类管理
 * @author Administrator
 *
 */

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	@Override
	public List<EasyUITreeNode> getContentgoryList(Long parentId) {
		//根据parentId查询子节点列表
		TbContentCategoryExample example=new TbContentCategoryExample();
		//设置查询条件
		Criteria criteria=example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list=contentCategoryMapper.selectByExample(example);
		List<EasyUITreeNode> resultList=new ArrayList<EasyUITreeNode>();
		for (TbContentCategory tbContentCategory : list) {
			EasyUITreeNode node=new EasyUITreeNode();
			node.setId(tbContentCategory.getId());
			node.setText(tbContentCategory.getName());
			node.setState(tbContentCategory.getIsParent()?"closed":"open");
			resultList.add(node);
		}
		return resultList;
	}

	public TaotaoResult addContentCategory(Long parentId,String name){
		TbContentCategory contentCategory=new TbContentCategory();
		contentCategory.setParentId(parentId);
		contentCategory.setName(name);
		//1正常，2不正常，删除
		contentCategory.setStatus(1);
		//排序默认为1
		contentCategory.setSortOrder(1);
		contentCategory.setIsParent(false);
		contentCategory.setCreated(new Date());
		contentCategory.setUpdated(new Date());
		contentCategoryMapper.insert(contentCategory);
		//判断父节点状态
/*		TbContentCategoryExample example=new TbContentCategoryExample();
		TbContentCategoryExample.Criteria criteria=example.createCriteria();
		criteria.*/
		TbContentCategory parent=contentCategoryMapper.selectByPrimaryKey(parentId);
		if(!parent.getIsParent()){
			parent.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKeySelective(parent);
		}
		return TaotaoResult.ok(contentCategory);
		
	}

	@Override
	public TaotaoResult updateContentCategory(Long id, String name) {
		TbContentCategory contentCategory=new TbContentCategory();
		contentCategory.setId(id);
		contentCategory.setName(name);
		contentCategory.setUpdated(new Date());
		contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult deleteContentCategory(Long id) {
		TbContentCategory contentCategory=new TbContentCategory();
		contentCategory=contentCategoryMapper.selectByPrimaryKey(id);
		/*		if(contentCategory.getIsParent()){
			//递归删除
			TbContentCategoryExample example=new TbContentCategoryExample();
			TbContentCategoryExample.Criteria criteria=example.createCriteria();
			criteria.andParentIdEqualTo(id);
			List<TbContentCategory> categoryList=contentCategoryMapper.selectByExample(example);
			

			return TaotaoResult.notOk();
		}else{
			contentCategoryMapper.deleteByPrimaryKey(id);
			return TaotaoResult.ok();
		}*/
		delCategory(contentCategory);
		return TaotaoResult.ok();
	}
	
	public void delCategory(TbContentCategory contentCategory){
		if(contentCategory.getIsParent()){
			//递归删除
			TbContentCategoryExample example=new TbContentCategoryExample();
			TbContentCategoryExample.Criteria criteria=example.createCriteria();
			criteria.andParentIdEqualTo(contentCategory.getId());
			List<TbContentCategory> categoryList=contentCategoryMapper.selectByExample(example);
			if(categoryList==null||categoryList.size()<=0){
				contentCategoryMapper.deleteByPrimaryKey(contentCategory.getId());	
			}else{
			for (TbContentCategory tbContentCategory : categoryList) {
				delCategory(tbContentCategory);
			}
			}
		}else{
			contentCategoryMapper.deleteByPrimaryKey(contentCategory.getId());	
		}
		
	}
}
